package com.example.shop.order.service.impl;

import com.example.shop.common.api.ServiceReturn;
import com.example.shop.common.api.ServiceStateCode;
import com.example.shop.order.dao.OrderBrowseInfoDao;
import com.example.shop.order.dao.OrderDetailInfoDao;
import com.example.shop.order.dto.OrderBrowseInfo;
import com.example.shop.order.dto.OrderDetailInfo;
import com.example.shop.order.dto.OrderQueryParam;
import com.example.shop.order.service.BrowseOrderService;
import com.example.shop.pojo.Merchant;
import com.example.shop.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BrowseOrderServiceImpl implements BrowseOrderService {

    private final OrderBrowseInfoDao browseInfoDao;
    private final OrderDetailInfoDao detailInfoDao;

    public BrowseOrderServiceImpl(OrderBrowseInfoDao browseInfoDao, OrderDetailInfoDao detailInfoDao) {
        this.browseInfoDao = browseInfoDao;
        this.detailInfoDao = detailInfoDao;
    }

    @Override
    public ServiceReturn<List<OrderBrowseInfo>> merchantObtainOrderInfo(Merchant merchant, OrderQueryParam queryParam) {
        List<OrderBrowseInfo> browseList = browseInfoDao.selectMerchantOrderList(merchant.getMerchantId(), queryParam);
        return new ServiceReturn<>(ServiceStateCode.SUCCESS,browseList);
    }

    @Override
    public ServiceReturn<OrderDetailInfo> merchantObtainOrderInfo(Merchant merchant, long orderId) {
        OrderDetailInfo detailInfo = detailInfoDao.selectMerchantOrderById(merchant.getMerchantId(), orderId);
        if(detailInfo!=null){
            return new ServiceReturn<>(ServiceStateCode.SUCCESS,detailInfo);
        }
        return new ServiceReturn<>(ServiceStateCode.NO_SUCH_RECORD,null);
    }

    @Override
    public ServiceReturn<OrderDetailInfo> obtainDetailInfo(User user, long orderId) {
        OrderDetailInfo detailInfo = detailInfoDao.selectById(user.getUserId(), orderId);
        if(detailInfo!=null){
            return new ServiceReturn<>(ServiceStateCode.SUCCESS,detailInfo);
        }
        return new ServiceReturn<>(ServiceStateCode.NO_SUCH_RECORD,null);
    }

    @Override
    public ServiceReturn<List<OrderBrowseInfo>> obtainBrowseList(User user, OrderQueryParam queryParam) {
        List<OrderBrowseInfo> browseList = browseInfoDao.select(user.getUserId(), queryParam);
        return new ServiceReturn<>(ServiceStateCode.SUCCESS,browseList);
    }
}
